package com.gx.service.impl;

import com.alibaba.fastjson.JSON;
import com.gx.domain.SysDataDict;
import com.gx.domain.SysDataDictExample;
import com.gx.id.DefaultIdGenerator;
import com.gx.id.IdGenerator;
import com.gx.mapper.SysDataDictMapper;
import com.gx.response.tree.BootstrapUiTreeNode;
import com.gx.service.api.SysDataDictService;
import com.gx.utils.ResourceToTreeNodeConvert;
import com.gx.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SysDataDictImpl implements SysDataDictService {

    private IdGenerator idGenerator = DefaultIdGenerator.getInstance();

    @Autowired private SysDataDictMapper sysDataDictMapper;

    @Override
    public int save(SysDataDict sysDataDict) {
        sysDataDict.setId(idGenerator.generateId());
        sysDataDict.setCreateTime(new Date());
        sysDataDict.setUpdateTime(new Date());
        return sysDataDictMapper.insertSelective(sysDataDict);
    }

    @Override
    public int update(SysDataDict sysDataDict) {
        SysDataDictExample sysDataDictExample = new SysDataDictExample();
        SysDataDictExample.Criteria criteria = sysDataDictExample.createCriteria();
        criteria.andIdEqualTo(sysDataDict.getId());
        sysDataDict.setUpdateTime(new Date());
        log.info("入参："+JSON.toJSONString(sysDataDict));
        return sysDataDictMapper.updateByExampleSelective(sysDataDict,sysDataDictExample);
    }

    @Override
    public int delete(Long id) {
        SysDataDictExample sysDataDictExample = new SysDataDictExample();
        SysDataDictExample.Criteria criteria = sysDataDictExample.createCriteria();
        criteria.andIdEqualTo(id);
        return sysDataDictMapper.deleteByExample(sysDataDictExample);
    }

    @Override
    public List<BootstrapUiTreeNode> tree() {
        List<BootstrapUiTreeNode> bootstrapUiTreeNodes = new ArrayList<>();
        SysDataDictExample sysDataDictExample = new SysDataDictExample();
        SysDataDictExample.Criteria criteria = sysDataDictExample.createCriteria();
        criteria.andDelFlagEqualTo(0);
        List<SysDataDict> listSysDataDict = sysDataDictMapper.selectByExample(sysDataDictExample);
        log.info("list:"+JSON.toJSONString(listSysDataDict));
        List<SysDataDict> root = ResourceToTreeNodeConvert.getRootNodesList(listSysDataDict);
        log.info("root:"+JSON.toJSONString(root));
        bootstrapUiTreeNodes = ResourceToTreeNodeConvert.convertClassListToSelectBootstrapUiTreeNodeList(root,listSysDataDict);
        log.info("bootstrapUiTreeNodes:"+JSON.toJSONString(bootstrapUiTreeNodes));
        return bootstrapUiTreeNodes;
    }

    @Override
    public List<SysDataDict> queryByCode(String roleSessionName, SysDataDict sysDataDict) {
        SysDataDictExample sysDataDictExample = new SysDataDictExample();
        SysDataDictExample.Criteria criteria = sysDataDictExample.createCriteria();
        criteria.andRoleSessionNameEqualTo(roleSessionName);
        criteria.andFieldCodeEqualTo(sysDataDict.getFieldCode());
        criteria.andDelFlagEqualTo(0);
        sysDataDictExample.setOrderByClause("sort");
        return sysDataDictMapper.selectByExample(sysDataDictExample);
    }

    @Override
    public List<SysDataDict> queryList(SysDataDict sysDataDict) {
        SysDataDictExample sysDataDictExample = new SysDataDictExample();
        SysDataDictExample.Criteria criteria = sysDataDictExample.createCriteria();
        if (StringUtils.isNotEmpty(sysDataDict.getFieldCode())) {
            criteria.andFieldCodeLike("%" + sysDataDict.getFieldCode() + "%");
        }
        if (StringUtils.isNotEmpty(sysDataDict.getItemName())) {
            criteria.andItemNameLike("%" + sysDataDict.getItemName() + "%");
        }
        if (StringUtils.isNotEmpty(sysDataDict.getRoleSessionName())) {
            criteria.andRoleSessionNameLike("%" + sysDataDict.getRoleSessionName() + "%");
        }
        criteria.andDelFlagEqualTo(0);
        List<SysDataDict> list = sysDataDictMapper.selectByExample(sysDataDictExample);
        log.info(JSON.toJSONString(list));
        return list;
    }
}
